﻿Public Class ProductExplorer
    Inherits ExplorerTreeList

    Protected EditPanePL As PLBase

    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)
    End Sub

    Public Sub New()
        MyBase.New()
    End Sub

    Public Overrides Sub PopulateDetailSection()
        Me.Cursor = Cursors.WaitCursor
        '
        ' Memory management / Help the GC
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If

        Dim guidEnfant As Guid
        Dim aPLName As String

        Dim DescriptionLang As String = "_PRM"

        Dim aPaneMetaQueryName As String
        Dim Ametaquerie As MetaQuery
        Dim frmCaption As String = "Product"        'DeskTopManager.GetMessage("CONNECT") to be implemented when metastring has code

        Try
            guidEnfant = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
            aPLName = DirectCast(NodeDataRow("PL"), String)

            aPaneMetaQueryName = DirectCast(NodeDataRow("MetaQuerie"), String)
            Ametaquerie = MetaQuery.GetMetaQueryByName(aPaneMetaQueryName)

            'c'est ici qu'on bind les info du noeud qui articule le Edit PANE
            EditPanePL = PLBase.CreateInstanceOf(aPLName, aPaneMetaQueryName)
            '   EditPanePL.SQLWhere = String.Format("ProductGroupID_FK IN ({0})", AppManager.GetGCCTSSecurity(IdRepository.ALLOWREAD, IdRepository.ProductGCCTS("GroupID")))

            aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = EditPanePL, .SecureGrid = True, .Name = Ametaquerie.Caption, .FormCtrlCD = MyBase.PanelName}
            If Not Guid.Empty.Equals(guidEnfant) Then
                EditPanePL.SQLWhere = String.Format("{0} = '{1}'", Ametaquerie.GUIDField, guidEnfant)
                If AppManager.ApplicationOption.DescriptionColumnIndex = 3 Then
                    DescriptionLang = "_ALT"
                End If

                If NodeDataRow.Table.Columns.Contains("Code") AndAlso NodeDataRow.Table.Columns.Contains("Description" & DescriptionLang) Then
                    frmCaption = String.Format("{0} - {1}", NodeDataRow("Code"), NodeDataRow("Description" & DescriptionLang))
                Else
                    frmCaption = "Code, Description" & DescriptionLang
                End If

            End If
        Catch ex As Exception
            MessageWindow.ShowMessage(ex)
        End Try

        Select Case FocusedNodeLevel
            Case 0
                AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewMasterRow
            Case Else
                AddHandler aGridControl.InitNewMasterRow, AddressOf InitNewMasterRow
        End Select
        '
        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Text = frmCaption
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next
        Me.Cursor = Cursors.Default
    End Sub







    Private Sub ProductExplorer_Load(sender As Object, e As EventArgs) Handles Me.Load
        TreeActionOptionListLoad()
        MetaControlTreeListActionOptionList.ApplySecurity()
    End Sub


    Private Sub Product_GridLayout()
        Try

            Me.Cursor = Cursors.WaitCursor

            PL = New SystemBoxSDK.PL.CoreSDK.Product_PL("PC_Products")
            aGridLayout = New GridLayoutPL() With {.Dock = DockStyle.Fill, .Pl = PL, .FormCtrlCD = Nothing}

            For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
                If aFrm.Name = PanelName Then
                    aFrm.Controls.Clear()
                    aFrm.Controls.Add(aGridLayout)
                    aFrm.Text = aGridLayout.Pl.QueryName
                    aFrm.Show()
                End If
                aFrm.Refresh()
            Next

            AddHandler aGridLayout.InitNewMasterRow, AddressOf InitNewLayoutRow

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            MessageWindow.ShowMessage(ex, MsgBoxStyle.Information)
        End Try
    End Sub



End Class
